< previous page page_188 next page >

Page 188
The revised EnumerateValues function uses the CurrentValueLength variable to handle this change. The variable is loaded with the buffer length and passed to the API function. On return, it contains the actual length of the value name. Since we have the length, there is no need to use the nulloffset variable and Instr function to determine the real string length. The CurrentValueLength variable must be reset to the maximum buffer size before each call to RegEnumValue, as follows:
Private Declare Function RegEnumValue Lib "advapi32.dll" _
Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, _
ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved _
As Long, ByVal lpType As Long, ByVal lpData As Long, ByVal lpcbData As _
Long) As Long


' Enumerate all of the values of this key, returning a collection
' containing the value names
Private Function EnumerateValues(ByVal hKey As Long) As Collection
   Dim NumberOfSubValues As Long
   Dim MaxSubValueLength As Long
   Dim res As Long
   Dim ValueIndex As Long
   Dim Valuebuffer As String
   Dim ValueCollection As New Collection
   Dim currentValue As clsKeyValues
   Dim CurrentValueLength As Long

   res = GetValueInfo(hKey, NumberOfSubValues, MaxSubValueLength)
   If res <> 0 Then
      Exit Function
   End If

   Valuebuffer = String$(MaxSubValueLength + 1, 0)

   For ValueIndex = 0 To NumberOfSubValues - 1
      CurrentValueLength = MaxSubValueLength + 1
      res = RegEnumValue(hKey, ValueIndex, Valuebuffer, CurrentValueLength, _
      0, 0, 0, 0)
      If res = 0 Then
         If CurrentValueLength = 0 Then
            ValueCollection.Add "Default"
         Else
            ValueCollection.Add Left$(Valuebuffer, CurrentValueLength)

 
< previous page page_188 next page >